#############################################################################
#                                                                           #
#   File: plugins/hosts.py                                                  #
#                                                                           #
#   Copyright (C) 2010-2011                                                 #
#                                                                           #
#   Home: http://code.google.com/p/you-proxy/                               #
#                                                                           #
#   This file is part of wallproxy-fixed.                                   #
#                                                                           #
#   Thanks to wallproxy. This software fixed some bug in wallproxy 1.0.4    #
#                                                                           #
#   wallproxy-fixed is free software: you can redistribute it and/or modify #
#   it under the terms of the GNU General Public License as                 #
#   published by the Free Software Foundation, either version 3 of the      #
#   License, or (at your option) any later version.                         #
#                                                                           #
#   wallproxy-fixed is distributed in the hope that it will be useful,      #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of          #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           #
#   GNU General Public License for more details.                            #
#                                                                           #
#   You should have received a copy of the GNU General Public License       #
#   along with wallproxy-fixed.  If not, see <http://www.gnu.org/licenses/>.#
#                                                                           #
#############################################################################

__author__ = 'd3d3LmVodXN0QGdtYWlsLmNvbQ=='.decode('base64')
__version__ = '0.0.2'

#from util import proxylib
import random

class Handler:
    def __call__(self, host):
        newhost = _map_hosts(host)
        return newhost!=host
    test = __call__

init_time = 0
plugin_name = 'local dns mapping'
hosts={}
def init_plugin(hosts_):
    import re
    global hosts
    re_split = re.compile(r'\s+').split
#    hosts = proxylib.hosts;
    c = len(hosts)
    hosts={}
    for line in hosts_.splitlines():
        line = line.strip().lower()
        if not line or line.startswith('#'): continue
        line = re_split(line, 1)
        if len(line) != 2: continue
        h = line[1];
        ip = line[0];
        if h in hosts:
		      hosts[h].append(ip);
        else:
	        hosts[h] = [ip];
    print ' updated %d dns mapping' % (len(hosts) - c)
    return Handler()


def _map_hosts(host):
  global hosts
  newhost = hosts.get(host)
  if newhost is None:
    i = host.find('.')
    if i != -1:
      newhost = hosts.get(host[i:])
  if newhost is not None:
#    print newhost
    return random.choice(newhost)
  return host
